<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      <el-form-item label="文章标题:">
        <el-input v-model="query.name" ></el-input>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select clearable filterable v-model="query.status" >
          <el-option label="已删除" :value="0"></el-option>
          <el-option label="未审核" :value="1"></el-option>
          <el-option label="已审核" :value="2"></el-option>
          <el-option label="审核未通过" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'article:search'" icon='el-icon-search' type="primary" @click="queryData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
      </el-form-item>
    </el-form>
      <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%">
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="title"
        label="文章标题"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="viewCount"
        label="浏览数"
        width="120">
      </el-table-column>
      <el-table-column
        align="center"
        prop="thumhup"
        label="点赞数"
        width="120">
      </el-table-column>
      <!-- 0: 不公开 1：公开 -->
      <el-table-column
        align="center"
        prop="ispublic"
        label="是否公开"
        width="120">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.ispublic === 1">公开</el-tag>
          <el-tag type="danger" v-show="scope.row.ispublic===0">不公开</el-tag>
        </template>                
      </el-table-column>
      <!-- 0: 已删除, 1:未审核，2:已审核,3: 审核未通过 -->
      <el-table-column
        align="center"
        prop="status"
        label="状态"
        width="120">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.status === 0" type="danger">已删除</el-tag>
          <el-tag  v-show="scope.row.status===1">未审核</el-tag>
          <el-tag type="success" v-show="scope.row.status===2">已审核</el-tag>
          <el-tag type="warning" v-show="scope.row.status===3">审核未通过</el-tag>
        </template>                
      </el-table-column>
      <el-table-column
        align="center"
        prop="updateDate"
        label="最后更新时间"
        width="180">
        <template slot-scope="scope">
            {{getFormat(scope.row.updateDate)}}
        </template>
      </el-table-column>
      <el-table-column  
        align="left"   
        label="操作"
        min-width="250">
        <template slot-scope="scope">
            <el-button
              v-permission="'article:view'"
              size="mini"
              type="primary"
              @click="look(scope.row.id)">查看</el-button>
            <el-button
              size="mini"
              type="success"
              v-permission="'article:audit'"
              @click="openAudit(scope.row.id)" v-show="scope.row.status===1">审核</el-button>
            <el-button
            size="mini"
            type="danger"
            v-permission="'article:delete'"
            @click="handleDelete(scope.row.id)" v-show="scope.row.status !== 0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>
    <audit :visible="audit.visible" :remoteClose="remoteClose" :title="audit.title" :id="audit.id" :isAudit="audit.isAudit"></audit>
  </div>
</template>

<script>
import api from '@/api/article'
import { format } from '@/utils/date'
import Audit from './audit.vue'
export default {
  name: 'Article',
  components: { Audit },
  data() {
    return {
      list:[],
      page:{ // 分页相关
        total:0, // 总记录数
        size:20,// 每页显示20条数据
        current:1// 当前页码
      },
      query:{},// 查询条件
      audit:{
        id:null,//文章id
        visible:false,
        title:'',
        isAudit:true//是否为审核
      }
    }
  },
  methods:{
    // 请求数据，获取类别
    fetchData(){
      api.getList(this.query,this.current,this.size).then(
        responce=>{
          this.list=responce.data.records
          this.page.total=responce.data.total
          
        }
      )
    },
    // 格式化时间
    getFormat(date) { 
      return format(date) 
    },

    // 查询功能
    queryData() {
        this.page.current=1
        this.fetchData()
    },
    // 重载
    reload(){
      this.query={}
      this.fetchData()
    },
    // 查看
    look(id){
      this.audit.id=id
      this.audit.visible=true
      this.audit.title='文章详情'
      this.audit.isAudit=false
    },
    // 审核
    openAudit(id){
      this.audit.id=id
      this.audit.visible=true
      this.audit.title='审核文章'
      this.audit.isAudit=true

    },
    // 删除
    handleDelete(id){
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.audit.visible=false
      
      this.fetchData()

    },
    //修改个数
    handleSizeChange(val) {       
        this.page.size=val
        this.fetchData()
    },

    // 修改当前页
    handleCurrentChange(val) {      
      this.page.current=val
      this.fetchData()
    },

  },
  created() {
    this.fetchData()
  },
}
</script>

<style>

</style>